<?php
/**
 * FullCalendar class file.
 * 
 */

Yii::import('zii.widgets.jui.CJuiWidget');

/**
 * FullCalendar displays a calendar widget.
 * 
 */
class FullCalendar extends CJuiWidget
{
    /** tag name */
    private $tagName = 'div';
    
    /** data provider */
    public $dataProvider;
    
    /** event update url */
    public $eventUpdateUrl;
    
    /** id field */
    public $idValue;
    
    /** title field */
    public $titleValue;

    /** tool tip field */
    public $toolTipValue;
    
    /** allDay field */
    public $allDayValue;
    
    /** start field */
    public $startValue;
    
    /** end field */
    public $endValue;
    
    /** url field */
    public $urlValue;
    
    /** className field */
    public $classNameValue;
    
    /** editable field */
    public $editableValue;
    
  
	/**
	 * Run this widget.
	 * This method registers necessary javascript and renders the needed HTML code.
	 */
	public function run()
	{
		$id=$this->getId();
		$this->htmlOptions['id']=$id;
		
		if (isset($this->themeUrl) && isset($this->theme))
		{
		  $this->options['theme']=true;
		}
		
		if (isset($this->eventUpdateUrl))
		{
		  $updateUrl = $this->evaluateExpression($this->eventUpdateUrl);
		  
		  $this->options['eventDrop'] = 'js:function(event,dayDelta,minuteDelta,allDay)
        {
          $.post("'.$updateUrl.'", { ajax: true, type: "move", id: event.id, dayDelta: dayDelta, minuteDelta: minuteDelta, allDay: allDay } );
        }
      ';
		  
		  $this->options['eventResize'] = 'js:function(event,dayDelta,minuteDelta)
        {
          $.post("'.$updateUrl.'", { ajax: true, type: "resize", id: event.id, dayDelta: dayDelta, minuteDelta: minuteDelta } );
        }
      ';
		}
		
		if (isset($this->dataProvider))
		{
		  $dataProviderEvents = array();
		  $i = 0;
		  foreach($this->dataProvider as $data)
		  {
		    if (isset($this->idValue))
		    {
		      $event['id'] = $this->evaluateExpression($this->idValue,array('data'=>$data));
		    }
		    else
		    {
		      $event['id'] = $i++;
		    }
		    
		    if (isset($this->titleValue))
		    {
		      $event['title'] = $this->evaluateExpression($this->titleValue,array('data'=>$data));
		    }

		    if (isset($this->toolTipValue))
		    {
		      $event['toolTip'] = $this->evaluateExpression($this->toolTipValue,array('data'=>$data));
		    }
		    
		    if (isset($this->allDayValue))
		    {
		      $event['allDay'] = $this->evaluateExpression($this->allDayValue,array('data'=>$data));
		    }
		    
		    if (isset($this->startValue))
		    {
		      $event['start'] = $this->evaluateExpression($this->startValue,array('data'=>$data));
		    }
		    
		    if (isset($this->endValue))
		    {
		      $event['end'] = $this->evaluateExpression($this->endValue,array('data'=>$data));
		    }
		    
		    if (isset($this->urlValue))
		    {
		      $event['url'] = $this->evaluateExpression($this->urlValue,array('data'=>$data));
		    }
		    
		    if (isset($this->editableValue))
		    {
		      $event['editable'] = $this->evaluateExpression($this->editableValue,array('data'=>$data));
		    }
		    
		    $dataProviderEvents[] = $event;
		  }
		  
		  $this->options['events'] = $dataProviderEvents;
		}

		echo CHtml::openTag($this->tagName, $this->htmlOptions);
		echo CHtml::closeTag($this->tagName);

		$clientScript = Yii::app()->clientScript;
		$assetManager = Yii::app()->assetManager;

    $assetDir = $assetManager->publish(dirname(__FILE__).DIRECTORY_SEPARATOR.'assets');
    $clientScript->registerCssFile($assetDir . '/css/fullcalendar.css');
    $clientScript->registerScriptFile($assetDir . '/js/fullcalendar.min.js');
    $clientScript->registerScriptFile($assetDir . '/js/gcal.js');
		
		$options = empty($this->options) ? '' : CJavaScript::encode($this->options);
		$clientScript->registerScript(__CLASS__.'#'.$id,"$('#{$id}').fullCalendar($options);");
	}
}
